home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3075 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.7 KB

  1. Path: hobbes.ece.uiuc.edu!user
  2. From: yang@ripley.ece.uiuc.edu (Weimin Yang)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Need help with overloading "<<"
  5. Date: Sun, 21 Jan 1996 21:17:31 +0700
  6. Organization: UIUC
  7. Message-ID: <yang-2101962117310001@hobbes.ece.uiuc.edu>
  8. References: <4duet0$ju9@wegener.ems.psu.edu>
  9. NNTP-Posting-Host: hobbes.ece.uiuc.edu
  10.  
  11. In article <4duet0$ju9@wegener.ems.psu.edu>, mahesh@cerse.psu.edu wrote:
  12.  
  13. >Hi !
  14. >
  15. >I was trying to overload the "<<" operator for a Complex class, 
  16. >without much sucess. Could someone please help me out ?
  17. >
  18. >In the main program, when I do 
  19. >
  20. >cout << a << endl ; ( here a is defined as   Complex a(1.0, 1.0) ;)
  21. >
  22. >the compiler gives the following error :
  23. >
  24. >"prog2.C", line 11.21: 1540-070: (S) Call does not match any argument list 
  25. >
  26. >for "ostream::operator<<".
  27. >
  28. >
  29. >The overloading function is :
  30. >
  31. >ostream& operator<<(ostream& s, Complex& z)
  32. >{
  33. > s << z.real() << " + " << z.img() <<"i" << endl;
  34. > return s;
  35. >} 
  36. >
  37. >
  38. >and the class definition is :
  39. >
  40. >// complex1.h
  41. >
  42. >class Complex
  43. >{
  44. >  private:
  45. >        
  46. >        double real_;
  47. >        double img_;
  48. >        
  49. >  public:
  50. >  
  51. >        Complex();                              // Constuctors
  52. >        Complex(double real, double img);
  53. >        
  54. >        Complex operator+ (Complex& z); // operations
  55. >        
  56. >        double real() {return real_;}           
  57. >        double img() {return img_ ;}
  58. >        
  59. >        void cprint();
  60. >        
  61. >        ~Complex() ;                    // destructor
  62. >        
  63. >}  ;
  64. >
  65. >
  66. >I have been stuck with this for some time now...
  67. >
  68. >
  69. >Thanks a lot
  70. >
  71. >Mahesh
  72.  
  73.  
  74.  
  75. you should put one line in your class definition:
  76.  
  77. friend ostream& operator<<(ostream& s, Complex& z);
  78.